import { getToken, removeToken, setToken } from '@/utils/auth'
import { logout, userLogin } from '@/api/sys/login'
const user = {
	state: {
		user: '',
		EX_SysAuthToken: getToken('EX_SysAuthToken'),
		menu: []
	},
	mutations: {
		SET_AUTHTOKEN: (state, EX_SysAuthToken) => {
			state.EX_SysAuthToken = EX_SysAuthToken
		},
		SET_JESSIONID: (state, EX_JESSIONID) => {
			state.EX_SysAuthToken = EX_JESSIONID
		}
	},
	actions: {
		userLogin({ commit }, userInfo) {
			const loginName = userInfo.loginName.trim()
			return new Promise((resolve, reject) => {
				userLogin(loginName, userInfo.password).then(response => {
					const data = response.data
					setToken('EX_SysAuthToken', response.data.EX_SysAuthToken)
					commit('SET_AUTHTOKEN', data.EX_SysAuthToken)
					resolve()
				}).catch(error => {
					console.log(error)
					reject(error)
				})
			})
		},
		LogOut({ commit, state }) {
			return new Promise((resolve, reject) => {
				logout(state.token).then(() => {
					commit('SET_AUTHTOKEN', '')
					removeToken('EX_SysAuthToken')
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		}
	}
}

export default user
